<? require("adm_header.php"); 

writeCommonAdminTable(LA_ADMIN_VSTAT,1);
echo "<table width='100%' cellpadding='0' cellspacing='0' class='bstyle'><tr><td>";

if (getParam("del","")!="")
{
	if (getParam("confirm","")!="")
	{
		echo "<p><b>Deleted</b><br>".LA_ADMIN_STAT_DELETED."</p>";
		q("delete from $stat_tbl");
	}
	else 
	{
		echo "<p><b><font color=red>".LA_ADMIN_STAT_DEL."</font></b><br><a href='adm_visitor_stat.php?del=1&confirm=1'>".LA_ADMIN_FTPL_DEL."</a></p>";
	}
	
}

echo "<b>".LA_ADMIN_VSTAT."</b><br>";
echo "<a href='adm_visitor_stat.php'>".LA_ADMIN_VSTAT_ALLYEARSMONTHS."</a> | <a href='adm_visitor_stat.php?del=1'>".LA_ADMIN_VSTAT_DELALL."</a><p>";
?>

<p>
<form method="GET" action="adm_visitor_stat.php">
<table class='noBorder'>
<tr><td><? echo LA_ADMIN_YEAR ?></td><td><? echo LA_ADMIN_MONTH ?></td><td><? echo LA_ADMIN_DAY ?></td></tr>
<tr><td>
<select name="year">
<? 
$arrayYear[2005]="2004";
$arrayYear[2005]="2005";
$arrayYear[2006]="2006";
$arrayYear[2007]="2007";
echo getDDFromList($arrayYear,getParam("year",date("Y"))); 
?>
</select> 
</td><td>
<select name="month">
<?
$array["99"]="All";
$array["01"]="Janary";
$array["02"]="February";
$array["03"]="March";
$array["04"]="April";
$array["05"]="May";
$array["06"]="June";
$array["07"]="July";
$array["08"]="August";
$array["09"]="September";
$array["10"]="October";
$array["11"]="November";
$array["12"]="December";
echo getDDFromList($array,getParam("month",date("m"))); 
?>
</select> 
</td>

<td>
<select name="day">
<?

$counter = 01;
$dayArray[99]="All";
while ($t<31)
{
	if (strlen($counter)==1)
		$counter = "0" . $counter;
	$dayArray[$counter]=$counter;
	$counter++;
	$t++;
}
echo getDDFromList($dayArray,getParam("day",date("d"))); 
?>
</select> 
</td>


<td>
<input type="submit" name="submit" value="<? echo LA_ADMIN_VSTAT_GET_STAT ?>">
</td></tr></table>
</form>
</p>

<?
$year=getParam("year","");
$month=getParam("month","");
$day=getParam("day","");

if ($year) {
	$time=" AND YEAR(visit)= '".$year . "'";
}
if ($month && $month !=99) {
	$time=" AND MONTH(visit)= '".$month . "'";
}
if ($day && $day !=99) {
	$time=" AND DAYOFMONTH(visit)= '".$day . "'";
}


if (isset($_GET["order_by"]))
	$order_by = $_GET["order_by"];
else
	$order_by = "visits";

if (isset($_GET["dir"]))
	$dir = $_GET["dir"];
else
	$dir = "desc";	

$common = "&year=$year&month=$month&day=$day";
?>
<table width="100%" class='noBorder'><tr class='trHeadLine'><td class='navlink'><b><? echo LA_ADMIN_VSTAT_PAGENAME ?></b></td><td><b><a class='navlink' href="?order_by=page&dir=asc<? echo $common; ?>"><u><? echo LA_ADMIN_VSTAT_PAGE ?></u></a></b></td><td><b><a class='navlink' href="?order_by=visits&dir=desc<? echo $common; ?>"><u><? echo LA_ADMIN_VSTAT_TOTAL ?></u></a></b></td><td><b><a class='navlink' href="?order_by=members&dir=desc<? echo $common; ?>"><u><? echo LA_ADMIN_VSTAT_MEMBERS ?></u></a></b></td><td><b><a class='navlink' href="?order_by=guests&dir=desc<? echo $common; ?>"><u><? echo LA_ADMIN_VSTAT_GUESTS ?></u></a></b></td></tr>
<?


$sql = "select YEAR(visit) as yr,DAYOFMONTH(visit) as d,MONTH(visit) as m, count(id) as visits,page,sum(member) as members,(count(id)-sum(member)) as guests from $stat_tbl where 2>1 $time group by page order by $order_by $dir";
$res = mysql_query($sql);

if (isset($_GET["export"]))
{
	$fields = mysql_num_fields($res); 
	
}

while ($row=mysql_fetch_array($res))
{
		$visits = $row["visits"];
		$page = $row["page"];
		$members = $row["members"];
		$guests = $row["guests"];
		
		$header .= mysql_field_name($res, $i) . "\t"; 	
		
		$name = "";
		
		if ($page == "latestads.php")
			$name = "Latest Ads list";
		else if ($page == "mostviewed.php")
			$name = "Most Viewed Ads list";
		else if ($page == "best_rated.php")
			$name = "Best Rated Ads list";
		else if ($page == "fav.php")
			$name = "Member: Favorites list";
		else if ($page == "member_login.php")
			$name = "Member: Login";
		else if ($page == "choose_cat.php")
			$name = "Member: Choose Category";
		else if ($page == "detail.php")
			$name = "Ad Details";
		else if ($page == "search.php")
			$name = "Search";
		else if ($page == "member.php")
			$name = "Member: Homepage";
		else if ($page == "picturebrowse.php")
			$name = "Picture Gallery";
		else if ($page == "register.php")
			$name = "Sign Up Page";
		else if ($page == "my_items.php")
			$name = "Member: My Items";
		else if ($page == "index.php")
			$name = "Front Page";
		else if ($page == "useritems.php")
			$name = "My Ads";
		else if ($page == "payment_options.php")
			$name = "Payment Options";
		else if ($page == "change.php")
			$name = "Change About me";
		else if ($page == "item.php")
			$name = "Add Ad/Edit Ad";
		else if ($page == "member_changepass.php")
			$name = "Change Password";
		else if ($page == "upload_file.php")
			$name = "Upload Picture";
		else if ($page == "latestwap.php")
			$name = "WAP";
		else if ($page == "gw_moneybookers.php")
			$name = "Member: Moneybookers";
		else
			$name = "Unknown";
		
		$color=colorSwitch($color);
			
		echo "<tr class='$color'><td>$name</td><td>$page</td><td>$visits</td><td>$members</td><td>$guests</td></tr>";
		
		
}

if (isset($_GET["export"]))
{
	while($row = mysql_fetch_row($res)) 
	{ 
	    $line = ''; 
	    foreach($row as $value) {                                             
	        if ((!isset($value)) OR ($value == "")) { 
	            $value = "\t"; 
	        } else { 
	            $value = str_replace('"', '""', $value); 
	            $value = '"' . $value . '"' . "\t"; 
	        } 
	        $line .= $value; 
	    } 
	    $data .= trim($line)."\n"; 
	} 
	$data = str_replace("\r","",$data); 
}


?>
</table>
</p>

<table width="600" class='noBorder'><tr><td><b><? echo LA_ADMIN_VSTAT_REFEREER ?></b></td><td><b><? echo LA_ADMIN_VSTAT_VISITS ?></b></td></tr>

<?

	
$sql = "select count(referer) as num_hits,referer from $stat_tbl WHERE 2>1 $time group by referer order by num_hits desc limit 50";
$res = mysql_query($sql);

while ($row=mysql_fetch_array($res))
{
		$visits = $row["num_hits"];
		$referer_full = $row["referer"];
		$referer = substr($row["referer"],0,60);
		
		if (!$referer)
			$referer = "Direct / Not known";	
		else
			$referer = "<a target='new' href='$referer_full'>$referer</a>";
			
		echo "<tr><td>$referer</a></td><td>$visits</td></tr>";
}
?>
</table>
</p>
<?
$array[1]="Jan";
$array[2]="Feb";
$array[3]="Mar";
$array[4]="Ap";
$array[5]="May";
$array[6]="Jun";
$array[7]="Jul";
$array[8]="Aug";
$array[9]="Sep";
$array[10]="Oct";
$array[11]="Nov";
$array[12]="Dec";
$this_was_year="";


$sql_total="select count(*) as total_users_joined from $stat_tbl order by visit  limit 12";
$row=mysql_fetch_array(q($sql_total));
$lTotal = $row["total_users_joined"];
echo "<table width='100%'><tr>";
$sql="select count(*) as num_users_month, page,YEAR(visit) as yr, MONTH(visit) as mo from $stat_tbl group by yr,mo order by visit desc";
$res=q($sql);
while ($row=mysql_fetch_array($res))
{
		$lNumAdsThisMonth=$row["num_users_month"];
		$lYear=$row["yr"];
		$lMonth=$row["mo"];
		
		if ($lNumAdsThisMonth>0)
		{
			$lLength=round(($lNumAdsThisMonth/$lTotal)*100);
			$lLength_bar=$lLength*3;
		}
		else 
		{
			$lLength=0;
			$lLength_bar=0;
		}
		echo "<td><table><tr><td><img src='../layout_images/graph.gif' align='left' width='5' height='$lLength_bar' alt='$lLength% of all pageviews last 12 months' /></td></tr><tr><td>$lYear<br>".$array[$lMonth]."</td></tr></table></td>";
		$this_was_year=$lYear;
}
echo "</td></table>";
echo "</td></tr></table>";
writeCommonAdminTable("Visitor Statistics","");
require("adm_footer.php"); 
?>